package com.yzc.mihuyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzc.mihuyou.controller.PingiveController;
import com.yzc.mihuyou.entity.Comment;
import com.yzc.mihuyou.entity.PageWuer;
import com.yzc.mihuyou.entity.Pingive;
import com.yzc.mihuyou.entity.User;
import com.yzc.mihuyou.mapper.CommentMapper;
import com.yzc.mihuyou.mapper.PingiveMapper;
import com.yzc.mihuyou.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.mihuyou.service.PingiveService;
import com.yzc.mihuyou.service.UserService;
import com.yzc.mihuyou.service.UserimgService;
import com.yzc.mihuyou.util.CommentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author 易紫诚
 * @since 2022-04-28
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserService userService;
    @Autowired
    UserimgService userimgService;
    @Autowired
    PingiveService pingiveService;
    @Override
    public Map<String,Object> selCome(int id,PageWuer pageWuer,Integer type,Long uid,Integer userId) {
        System.out.println(userId);
        Map<String,Object> map=new HashMap<>();
        int ter=new Long(pageWuer.getCurrent()).intValue();
        int tes=new Long(pageWuer.getSize()).intValue();
        Page<Comment> page=new Page<>(ter,tes);
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<>();
//       热门
        queryWrapper.eq("blog_id",id);
        queryWrapper.eq("root_parent_id",0);
        queryWrapper.eq("root_parent_id",0);
        queryWrapper.eq("giv_we",0);
        if(userId!=null){
            queryWrapper.eq("user_id",userId);
        }
        if(type==1){
            queryWrapper.orderByDesc("give_num");
        }else if(type==2) {
//            最新
            queryWrapper.orderByDesc("gmt_create");
        }else if(type==3){
//            最早
            queryWrapper.orderByAsc("gmt_create");
        }
        List<Comment> comments=commentMapper.selectPage(page,queryWrapper).getRecords();
        System.out.println(comments);
        for (int q=0;q<comments.size();q++){
            comments.get(q).setUser(userService.cuw(comments.get(q).getUserId()));
            comments.get(q).setTrie(pingiveService.inbut(comments.get(q).getCommentId(),uid));

            QueryWrapper<Comment> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("root_parent_id",comments.get(q).getCommentId());
            queryWrapper1.eq("giv_we",0);
            queryWrapper1.orderByDesc("give_num");
            comments.get(q).setChild(commentMapper.selectList(queryWrapper1));
            for(int i=0;i<comments.get(q).getChild().size();i++){
                comments.get(q).getChild().get(i).setUser(userService.cuw(comments.get(q).getChild().get(i).getUserId()));
                comments.get(q).getChild().get(i).setTrie(pingiveService.inbut(comments.get(q).getChild().get(i).getCommentId(),uid));
                if(!comments.get(q).getChild().get(i).getParentId().equals(comments.get(q).getChild().get(i).getRootParentId())){
                    comments.get(q).getChild().get(i).setUwer2(userService.getById(commentMapper.selectById(comments.get(q).getChild().get(i).getCommentId())));
                }
            }
        }
        PageWuer pages=new PageWuer(page.getTotal(),page.getSize(),page.getCurrent(),page.getPages());
        map.put("ListPin",comments);
        map.put("pages",pages);
        return map ;
    }

    @Override
    public int addCome(Comment comment) {
        commentMapper.insert(comment);
        return comment.getCommentId();
    }

    @Override
    public Comment serCwu(int id) {
        Comment comment=commentMapper.selectById(id);
        comment.setTrie(false);
        if(comment.getRootParentId()>0 && comment.getRootParentId()!=comment.getParentId()){
            comment.setUwer2(userService.cuw(commentMapper.selectById(comment.getParentId()).getUserId()));
            comment.setUser(userService.cuw(comment.getUserId()));
        }
        return comment;
    }

    @Override
    public int delect(int id) {
        Comment comment=commentMapper.selectById(id);
        if(comment!=null){
            if(comment.getRootParentId()>0){
                comment.setGivWe(1);
                commentMapper.updateById(comment);
            }else {
                QueryWrapper queryWrapper=new QueryWrapper();
                queryWrapper.eq("root_parent_id",id);
                queryWrapper.or();
                queryWrapper.eq("comment_id",id);
                commentMapper.delete(queryWrapper);
            }
        }else {
            return  1;
        }
        return 1;
    }

    @Override
    public List<Comment>  getComment(long id) {
        return commentMapper.getComment(id);
    }

    @Override
    public int upIue(int comId, Boolean b) {
        Comment comment=commentMapper.selectById(comId);
        if(b){
            comment.setGiveNum(comment.getGiveNum()+1);
        }else {
            comment.setGiveNum(comment.getGiveNum()-1);
        }
        return commentMapper.updateById(comment);
    }



}
